<?php


namespace App\HttpController\Admin;



use App\Consts\RedisKey;
use App\Middleware\AuthMiddleware;
use App\Model\Role;
use App\Utility\Response;
use EasySwoole\EasySwoole\Config;
use EasySwoole\RedisPool\Redis;

class Authority extends AdminBaseController
{

    public function index(){

        $list = \App\Model\Authority::create()->all();
        $data = [];
        foreach ($list as $item){
            $data[] = $item->toArray();
        }
        $menus =  make_tree($data,'id','pid','child');
        return $this->returnData(200,$menus);

    }

    public function create(){

        $params = $this->request()->getRequestParam();

        if($this->request()->getUploadedFiles()){
            $system_upload = Config::getInstance()->getConf('system_upload');
            $imgs =[ 'icon','icon_active'] ;
            foreach ($imgs as $name){
                $file = $this->request()->getUploadedFile($name);
                if($file){
                    $fileType = $file->getClientMediaType();
                    if(!in_array($fileType,$system_upload['ext'])){
                        return $this->returnData(1044);
                    }
                    if($file->getSize() > $system_upload['size'] ){
                        return $this->returnData(1045);
                    }
                    $file_name = md5($this->getUserId().date('YmdHis').mt_rand(10000,90000)).'.png';
                    $res = $file->moveTo(Config::getInstance()->getConf('MAIN_SERVER.SETTING.document_root').$system_upload['path'].$file_name);
                    if($res){
                        $params[$name] = $file_name;
                    }

                }
            }

        }
        $res = \App\Model\Authority::create()->data($params)->save();
        if($res){
            $this->updateRoleCache();
            return $this->returnData(200);
        }
        return $this->returnData(400);
    }

    public function edit(){


        $params = $this->request()->getRequestParam();

        if($this->request()->getUploadedFiles()){
            $system_upload = Config::getInstance()->getConf('system_upload');
            $imgs =[ 'icon','icon_active'] ;
            foreach ($imgs as $name){
                $file = $this->request()->getUploadedFile($name);
                if($file){
                    $fileType = $file->getClientMediaType();
                    if(!in_array($fileType,$system_upload['ext'])){
                        return $this->returnData(1044);
                    }
                    if($file->getSize() > $system_upload['size'] ){
                        return $this->returnData(1045);
                    }
                    $file_name = md5($this->getUserId().date('YmdHis').mt_rand(10000,90000)).'.png';
                    $res = $file->moveTo(Config::getInstance()->getConf('MAIN_SERVER.SETTING.document_root').$system_upload['path'].$file_name);
                    if($res){
                        $params[$name] = $file_name;
                    }

                }
            }

        }
        $res = \App\Model\Authority::create()->where(['id'=> $params['id']])->update($params);
        if($res){
            $this->updateRoleCache();
            return $this->returnData(200);
        }
        return $this->returnData(400);
    }

    public function updateRoleCache(){

        $role_ids = Role::create()->column('id');
        foreach ($role_ids as $role_id) {
            Redis::defer('redis')->set(RedisKey::user_authority_list($role_id), json_encode(AuthMiddleware::getInstance()->getAuthority($role_id)));
        }
    }



}